package Jiami;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @Author: GodChin
 * @Date: 2021/8/17 9:14
 * @Blog: itdfq.com
 * @QQ: 909256107
 * @Description: 使用非对称加密 RSA
 */
public class Demo2 {
    public static void main(String[] args) {
        RSA rsa = new RSA();
        //获得私钥
        rsa.getPrivateKey();
        rsa.getPrivateKeyBase64();
//        System.out.println(rsa.getPrivateKey());
//        System.out.println(rsa.getPrivateKeyBase64());
        //获得公钥
        rsa.getPublicKey();
//        System.out.println("公钥："+rsa.getPublicKey());
        rsa.getPublicKeyBase64();
//        System.out.println("公钥64："+rsa.getPublicKeyBase64());

        //公钥加密
        byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        //私钥解密
        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);


        //私钥加密
        byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        String encode = Base64.encode(encrypt2);
        System.out.println(encode);
//        System.out.println("私钥加密："+StrUtil.str(encrypt2,CharsetUtil.UTF_8));
        //公钥解密
        byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);

        System.out.println( StrUtil.str(decrypt2, CharsetUtil.UTF_8));
    }

    private static final String PPRIVATEKEY="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMWbHfeJ+ruCfF/K9plS2pI33uHoRBAK3T0/8WHfGvtjt+RFyNcG9Ncq1TFx+nzxO5GhfxqLtFeAv6xr2JCJxcmNRGKpjbXaDWV6grCYg8zx5yb805ul2ovWHiB2N1rvpyMmU4yyA9puX/3A4sdZDu8URzKIGa0/I/oO2X/WBGTpAgMBAAECgYB82wrvOnZ7ffIaEMMJk6lZFAY3FSvUOxkZN0rMPUzuJE6v5EwHWXTgc0brjAnqYf3ygWkvG7CiTU5OooHeqb6XAnlF+muLkoqnALunkfvEdFVlJmULujIi3JCki+JVFOnmzi3aovXsFFi8S84X8lvRlvDXxrEm6NZH2xJPFhITAQJBAPlH3V32eD26kqg50kBBTP7nUGB5sDQQd7Ye0t3LybJ5FC45KwsLH4obVGk8EyJ6pRKavcqs57DYwSYq6kSjSGECQQDK7q0GMSdN+xzjau/a5ox8dL3HLKJU+Y7LbwDCZfXUG1i6lfWOnDNJ+SXTDvgEYYiF0oWSIymPRpATgVkPX0mJAkAvh9RdpjEnE/wCt4OvXMQhllltC8bdJoas4xOx1rmOnieK6LucT3KkErH1ns7HNDtlYLvveSh0P+AUNvqUO84BAkEAoFM6zZaN5/UJmL6GqxnUSgttV+tygNH9N8oF/o/RTPch/Z+2Nio/9yRo9rtvxP5NfvSY5KD1F6X29AIQG637iQJAbf5EVHWcwKxGWLq3q6RxSA0KuDi7wfCqE1rtY6noXxVh3jVXlxWLx6PoCV5ena4cbzaKoCQuXS/rwmsmhRjGoQ==";
    private static final String PPUBLICKEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFmx33ifq7gnxfyvaZUtqSN97h6EQQCt09P/Fh3xr7Y7fkRcjXBvTXKtUxcfp88TuRoX8ai7RXgL+sa9iQicXJjURiqY212g1leoKwmIPM8ecm/NObpdqL1h4gdjda76cjJlOMsgPabl/9wOLHWQ7vFEcyiBmtPyP6Dtl/1gRk6QIDAQAB";
    @Test
    public void test3(){
        KeyPair pair = SecureUtil.generateKeyPair("RSA");
        PrivateKey aPrivate = pair.getPrivate();
        byte[] encoded = aPrivate.getEncoded();
        String encode = Base64.encode(encoded);
        System.out.println("私钥"+encode);
        PublicKey aPublic = pair.getPublic();
        String encode1 = Base64.encode(aPublic.getEncoded());
        System.out.println("公钥："+encode1);
    }

    @Test
    public void test1(){
        RSA rsa = new RSA(PPRIVATEKEY,PPUBLICKEY);
        String ceshi = rsa.encryptBase64("123", KeyType.PublicKey);

        System.out.println(ceshi);
        //aT8KyRNWAsuK1s52E+5h7ehPoXtagsYK0ie5rH+LKrHMf14nWT/RqrSfSKN/1Td9P5tkIZ7wIow59g0P32h0SKjEOqSohYkV9CLgCNXD69tg4pSWZGq+PYWRI30mEDNqSWclNPCXeetCpZ05gmIcGMpgd/Mv91/nT8T3zUGiS24=
    }
    @Test
    public void test2(){
        String mima="xAf9nm8Rdn13pj0dG5iEZMVex/l1OpDpDSNIiqBEMscAuW9IdAu8vTOy2WrRbwLLr4Z5NY875uE1KTrZdxLCREv4UJaABpxPAfZde1f8/QGd4D0ejVtvyQZjBwFVsK4aSrPOC01g9/A7uD20R8IPM8dMGJqaZ+DBHE0TgLzQ9Ms=";
        RSA rsa = new RSA(PPRIVATEKEY,PPUBLICKEY);
        byte[] decrypt = rsa.decrypt(mima, KeyType.PrivateKey);
        System.out.println(StrUtil.str(decrypt, StandardCharsets.UTF_8));
        String s = rsa.decryptStr(mima, KeyType.PrivateKey);
        System.out.println("解密的密码为："+s);
    }


}
